home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / sql / sqlex.ui.h.z / sqlex.ui.h
Encoding:
C/C++ Source or Header  |  2002-04-08  |  5.0 KB  |  150 lines

  1. /****************************************************************************
  2. ** ui.h extension file, included from the uic-generated form implementation.
  3. **
  4. ** If you wish to add, delete or rename slots use Qt Designer which will
  5. ** update this file, preserving your code. Create an init() slot in place of
  6. ** a constructor, and a destroy() slot in place of a destructor.
  7. *****************************************************************************/
  8. #include <qsqldriver.h>
  9. #include <qmessagebox.h>
  10. #include <qsqldatabase.h>
  11. #include <qlineedit.h>
  12. #include <qcombobox.h>
  13. #include <qspinbox.h>
  14. #include <qsqlerror.h>
  15. #include "connect.h"
  16.  
  17. class QCustomSqlCursor: public QSqlCursor
  18. {
  19. public:
  20.     QCustomSqlCursor( const QString & query = QString::null, bool autopopulate = TRUE, QSqlDatabase* db = 0 ) :
  21.         QSqlCursor( QString::null, autopopulate, db )
  22.     {
  23.     exec( query );
  24.     if ( isSelect() && autopopulate ) {
  25.         QSqlRecordInfo inf = ((QSqlQuery*)this)->driver()->recordInfo( *(QSqlQuery*)this );
  26.         for ( QSqlRecordInfo::iterator it = inf.begin(); it != inf.end(); ++it ) {
  27.         append( *it );
  28.         }        
  29.     }
  30.     setMode( QSqlCursor::ReadOnly );
  31.     }
  32.     QCustomSqlCursor( const QCustomSqlCursor & other ): QSqlCursor( other ) {}
  33.     bool select( const QString & /*filter*/, const QSqlIndex & /*sort*/ = QSqlIndex() )
  34.     { return exec( lastQuery() ); }
  35.     QSqlIndex primaryIndex( bool /*prime*/ = TRUE ) const
  36.     { return QSqlIndex(); }
  37.     int insert( bool /*invalidate*/ = TRUE )
  38.     { return FALSE; }
  39.     int update( bool /*invalidate*/ = TRUE )
  40.     { return FALSE; }
  41.     int del( bool /*invalidate*/ = TRUE )
  42.     { return FALSE; }
  43.     void setName( const QString& /*name*/, bool /*autopopulate*/ = TRUE ) {}
  44. };
  45.  
  46. static void showError( const QSqlError& err, QWidget* parent = 0 )
  47. {
  48.    QString errStr ( "The database reported an error\n" );
  49.     if ( !err.databaseText().isEmpty() )
  50.     errStr += err.databaseText();
  51.     if ( !err.driverText().isEmpty() )
  52.     errStr += err.driverText();
  53.     QMessageBox::warning( parent, "Error", errStr );
  54. }
  55. void SqlEx::init()
  56. {
  57.     hsplit->setResizeMode( lv, QSplitter::KeepSize );
  58.     vsplit->setResizeMode( gb, QSplitter::KeepSize );
  59.     submitBtn->setEnabled( FALSE );
  60. }
  61.  
  62. void SqlEx::dbConnect()
  63.     ConnectDialog* conDiag = new ConnectDialog( this, "Connection Dialog", TRUE );
  64.     if ( conDiag->exec() != QDialog::Accepted )
  65.     return;
  66.     if ( dt->sqlCursor() ) {
  67.     dt->setSqlCursor( 0 );
  68.     }
  69.     // close old connection (if any)
  70.     if ( QSqlDatabase::contains( "SqlEx" ) ) {
  71.     QSqlDatabase* oldDb = QSqlDatabase::database( "SqlEx" );
  72.     oldDb->close();
  73.     QSqlDatabase::removeDatabase( "SqlEx" );
  74.     }
  75.     // open the new connection
  76.     QSqlDatabase* db = QSqlDatabase::addDatabase( conDiag->comboDriver->currentText(), "SqlEx" );
  77.     if ( !db ) {
  78.     QMessageBox::warning( this, "Error", "Could not open database" );    
  79.     return;
  80.     }
  81.     db->setHostName( conDiag->editHostname->text() );
  82.     db->setDatabaseName( conDiag->editDatabase->text() );
  83.     db->setPort( conDiag->portSpinBox->value() );
  84.     if ( !db->open( conDiag->editUsername->text(), conDiag->editPassword->text() ) ) {
  85.     showError( db->lastError(), this );
  86.     return;
  87.     }
  88.     lbl->setText( "Double-Click on a table-name to view the contents" );
  89.     lv->clear();
  90.     
  91.     QStringList tables = db->tables();
  92.     for ( QStringList::Iterator it = tables.begin(); it != tables.end(); ++it ) {
  93.     QListViewItem* lvi = new QListViewItem( lv, *it );
  94.     QSqlRecordInfo ri = db->recordInfo ( *it );
  95.     for ( QSqlRecordInfo::Iterator it = ri.begin(); it != ri.end(); ++it ) {
  96.         QString req;
  97.         if ( (*it).isRequired() > 0 ) {
  98.         req = "Yes";
  99.         } else if ( (*it).isRequired() == 0 ) {
  100.         req = "No";
  101.         } else {
  102.         req = "?";
  103.         }
  104.         QListViewItem* fi = new QListViewItem( lvi, (*it).name(),  + QVariant::typeToName( (*it).type() ), req );
  105.         lvi->insertItem( fi );
  106.     }
  107.     lv->insertItem( lvi );    
  108.     }
  109.     submitBtn->setEnabled( TRUE );
  110. }
  111.  
  112. void SqlEx::execQuery()
  113. {
  114.     // use a custom cursor to populate the data table
  115.     QCustomSqlCursor* cursor = new QCustomSqlCursor( te->text(), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) );
  116.     if ( cursor->isSelect() ) {
  117.     dt->setSqlCursor( cursor, TRUE, TRUE );
  118.     dt->refresh( QDataTable::RefreshAll );
  119.     QString txt( "Query OK" );
  120.     if ( cursor->size() >= 0 )
  121.         txt += ", returned rows: " + QString::number( cursor->size() );
  122.     lbl->setText( txt );
  123.     } else {
  124.     // an error occured if the cursor is not active
  125.     if ( !cursor->isActive() ) {
  126.         showError( cursor->lastError(), this );
  127.     } else {
  128.         lbl->setText( QString("Query OK, affected rows: %1").arg( cursor->numRowsAffected() ) );
  129.     }
  130.     }
  131. }
  132.  
  133. void SqlEx::showTable( QListViewItem * item )
  134. {
  135.     // get the table name
  136.     QListViewItem* i = item->parent();
  137.     if ( !i ) {
  138.     i = item;
  139.     }
  140.  
  141.     // populate the data table
  142.     QSqlCursor* cursor = new QSqlCursor( i->text( 0 ), TRUE, QSqlDatabase::database( "SqlEx", TRUE ) );
  143.     dt->setSqlCursor( cursor, TRUE, TRUE );
  144.     dt->setSort( cursor->primaryIndex() );
  145.     dt->refresh( QDataTable::RefreshAll );
  146.     lbl->setText( "Displaying table " + i->text( 0 ) );
  147. }
  148.  
  149.